/*
 ============================================================================
 Name		: WebData.h
 Author	  : Vitaly Yevtushenko
 Version	 : 1.0
 Copyright   : Licensed under GPL V2
 Description : CWebData declaration
 ============================================================================
 */

#ifndef WEBDATA_H
#define WEBDATA_H

// INCLUDES
#include <e32std.h>
#include <e32base.h>

//constants
const TInt KVertexRadius(5);

// CLASS DECLARATION

/**
 *  CWebData
 * 
 */
class CWebData : public CBase
	{
public:
	// Constructors and destructor

	/**
	 * Destructor.
	 */
	~CWebData();

	/**
	 * Two-phased constructor.
	 */
	static CWebData* NewL();

	/**
	 * Two-phased constructor.
	 */
	static CWebData* NewLC();

	/**
	 * class contains two indexes of vertexes
	 */
	class TLine
		{
	public:
		inline TLine() {iP1 = iP2 = 0;}
		inline TLine(TInt aP1, TInt aP2) {iP1 = aP1; iP2 = aP2;}
		TInt iP1, iP2;
		};
	
	/**
	 * return count of lines in web
	 */
	inline TInt LinesCount() { if (iLines) return iLines->Count(); else return 0; }
	
	/**
	 * return coordinates of line
	 */
	TInt Line(const TInt aLineNum, TPoint &aP1, TPoint &aP2, TBool &aCrossed);

	/**
	 * return count of vertexes in web
	 */
	inline TInt PointsCount() { if (iVertexes) return iVertexes->Count(); else return 0; }
	
	/**
	 * return coordinates of vertex
	 */
	TInt Point(const TInt aPointNum, TPoint &aPoint, TBool &aTapped);
	
	/**
	 * Check if tapped point match one of the vertexes.
	 * Returns index of vertex or KErrNotFound.
	 */
	TInt PointTapped(const TPoint &aTapPoint); 
	
	/**
	 * Drags tapped point to specified position
	 */
	TInt DragTo(const TPoint &aPosition);
	
	/**
	 * Unhandle dragging point
	 */
	inline void StopDrag() { iTappedId = -1; }
	
	inline TBool Solved() { return iSolved; }
	
	void SaveL(const TFileName &aFileName);
	void LoadL(const TFileName &aFileName);

private:

	/**
	 * Constructor for performing 1st stage construction
	 */
	CWebData();

	/**
	 * EPOC default constructor for performing 2nd stage construction
	 */
	void ConstructL();
	
	/**
	 * Fill arrays with test data
	 */
	void TestWebDataL();
	
	/**
	 * Check lines are crossed.
	 * originally by http://users.livejournal.com/_winnie/152327.html
	 */
	TBool IsLinesCrossed(const TPoint &aP11, const TPoint &aP12, const TPoint &aP21, const TPoint &aP22);

	//coordinates of vertexes
	CArrayFix<TPoint> *iVertexes;
	//pairs of coordinate indexes - lines of web
	CArrayFix<TLine> *iLines;
	
	TBool iSolved;
	
	//tapped point index
	TInt iTappedId;
	};

#endif // WEBDATA_H
